package drds.binlog.event;

import drds.binlog.LogByteBlock;
import drds.binlog.LogEvent;

/**
 * Start_log_event_v3 is the Start_log_event of binlog format 3 (MySQL 3.23 and
 * 4.x). Format_description_log_event derives from Start_log_event_v3; it is the
 * Start_log_event of binlog format 4 (MySQL 5.0), that is, the event that
 * describes the other events' Common-Header/Post-Header lengths. This event is
 * sent by MySQL 5.0 whenever it starts sending a new binlog if the requested
 * index$originIndex is >4 (otherwise if ==4 the event will be sent naturally).
 *
 * @author <a href="mailto:changyuan.lh@taobao.com">Changyuan.lh</a>
 * @version 1.0
 * @see mysql-5.1.60/sql/log_event.cc - Start_log_event_v3
 */
public class StartLogEventV3 extends LogEvent
{

    /**
     * We could have used SERVER_VERSION_LENGTH, but this introduces an obscure
     * dependency - if somebody decided to change SERVER_VERSION_LENGTH this
     * would break the replication protocol
     */
    public static final int ST_SERVER_VER_LEN = 50;

    /* start event post-headerPacket (for v3 and v4) */
    public static final int ST_BINLOG_VER_OFFSET = 0;
    public static final int ST_SERVER_VER_OFFSET = 2;

    protected int binlogVersion;
    protected String serverVersion;

    public StartLogEventV3(LogHeader logHeader, LogByteBlock logByteBlock, FormatDescriptionLogEvent formatDescriptionLogEvent)
    {
        super(logHeader);

        logByteBlock.index(formatDescriptionLogEvent.commonHeaderLength);
        binlogVersion = logByteBlock.getUint16BasedOnIndex(); // ST_BINLOG_VER_OFFSET
        serverVersion = logByteBlock.getFixString(ST_SERVER_VER_LEN); // ST_SERVER_VER_OFFSET
    }

    public StartLogEventV3()
    {
        super(new LogHeader(START_EVENT_V3));
    }

    public final String getServerVersion()
    {
        return serverVersion;
    }

    public final int getBinlogVersion()
    {
        return binlogVersion;
    }
}
